package com.achievo.know.domain.infra.hibernate;

import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.achievo.know.domain.FileAttachment;
import com.achievo.know.domain.FileAttachmentRepository;
import com.dayatang.spring.repository.BaseEntityRepositoryHibernateSpring;

@Repository
public class FileAttachmentRepositoryImpl extends BaseEntityRepositoryHibernateSpring<FileAttachment,Long> implements FileAttachmentRepository {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public FileAttachmentRepositoryImpl() {
		super(FileAttachment.class);
	}

	@Override
	public List<FileAttachment> listAttachment(Long fkId,int i) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileAttachment.class);
		criteria.add(Restrictions.eq("fkId", fkId));
		criteria.add(Restrictions.eq("fkType", i));
		List<FileAttachment> list = getHibernateTemplate().findByCriteria(criteria);
		return list;
	}

	@Override
	public FileAttachment getImage(Long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileAttachment.class);
		criteria.add(Restrictions.eq("fkId", id));
		criteria.add(Restrictions.eq("fkType", 3));
		List<FileAttachment> list = getHibernateTemplate().findByCriteria(criteria);
		if(list.size()>0)
			return list.get(0);
		return null;
	}

	@Override
	public boolean exists(String baseName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileAttachment.class);
		criteria.add(Restrictions.eq("guid", baseName));
		List<FileAttachment> list = getHibernateTemplate().findByCriteria(criteria);
		if(list.size()>0)
			return true;
		return false;
	}
	
	@Override
	public FileAttachment existsFileName(String fileName) {
		fileName=FilenameUtils.getBaseName(fileName);
		DetachedCriteria criteria = DetachedCriteria.forClass(FileAttachment.class);
		criteria.add(Restrictions.eq("attachName", fileName));
		List<FileAttachment> list = getHibernateTemplate().findByCriteria(criteria);
		if(list.size()>0)
			return list.get(0);
		return null;
	}

	@Override
	public boolean existsPhoto(String name) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileAttachment.class);
		criteria.add(Restrictions.eq("photoName", name));
		List<FileAttachment> list = getHibernateTemplate().findByCriteria(criteria);
		if(list.size()>0)
			return true;
		return false;
	}

	@Override
	public FileAttachment getByGuid(String guid) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileAttachment.class);
		criteria.add(Restrictions.eq("guid", guid));
		List<FileAttachment> list = getHibernateTemplate().findByCriteria(criteria);
		if(list.size()>0)
			return list.get(0);
		return null;
	}

	@Override
	@Transactional
	public void deleteTempAttachments() {
		final String sql="DELETE FROM rams_temp_attachment";
		jdbcTemplate.execute(sql);
	}
	
	
}
